﻿using ALMDDD.Application.EventSourcing;
using ALMDDD.Application.Interfaces;
using ALMDDD.Application.Services;
using ALMDDD.Domain.CommandHandlers;
using ALMDDD.Domain.Commands.SysUser;
using ALMDDD.Domain.Core.Bus;
using ALMDDD.Domain.Core.Events.Interfaces;
using ALMDDD.Domain.Core.Notifications;
using ALMDDD.Domain.EventHandlers.SysUser;
using ALMDDD.Domain.Events;
using ALMDDD.Domain.Interfaces;
using ALMDDD.Domain.Interfaces.Repository;
using ALMDDD.Infrastructure.Bus;
using ALMDDD.Infrastructure.Context;
using ALMDDD.Infrastructure.Repository;
using ALMDDD.Infrastructure.Repository.EventSourcing;
using ALMDDD.Infrastructure.UnitWork;
using Autofac;
using Autofac.Core;
using Autofac.Extensions.DependencyInjection;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace ALMDDD.AutoFac
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceProvider ConvertToAutofac(
         this IServiceCollection services)
        {
            var container = new ContainerBuilder();
            //foreach (var module in modules)
            //{
            //    container.RegisterModule(module);
            //}
            //注入 ********应用层**************
            services.AddScoped<ISysUserService, SysUserService>();


            //注入 ********领域层**************
            services.AddScoped<INotificationHandler<DomainNotification>, DomainNotificationHandler>();
            services.AddScoped<INotificationHandler<SysUserAddEvent>, SysUserEventHandler>();
            services.AddScoped<IRequestHandler<AddUserCommand, Unit>, SysUserCommandHandler>();


            //注入 ********数据层**************
            services.AddScoped<ISysUserRepository, SysUserRepository>();
            services.AddScoped<ALMContext>();
            services.AddScoped<IunitOfWork, UnitOfWork>();

            //注入 ********领域核心层 -命令总线**************
            services.AddScoped<IMeDiatorHandler, InMemoryBus>();
            // 注入  ********事件溯源**************
            services.AddScoped<IEventStoreRepository, EventStoreRepository>();
            services.AddScoped<IEventStoreService, SqlEventStoreService>();
            services.AddScoped<EventStoreSQLContext>();
        

            //注入
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();


            container.Populate(services);
            return new AutofacServiceProvider(container.Build());
        }
    }
}
